package com.example.app02

import androidx.lifecycle.ViewModel

class StopwatchViewModel : ViewModel() {
    private var startTime: Long = 0L
    private var elapsedTime: Long = 0L
    var isRunning = false
        private set

    fun start() {
        if (!isRunning) {
            startTime = System.currentTimeMillis() - elapsedTime
            isRunning = true
        }
    }

    fun pause() {
        if (isRunning) {
            elapsedTime = System.currentTimeMillis() - startTime
            isRunning = false
        }
    }

    fun reset() {
        isRunning = false
        elapsedTime = 0L
        startTime = 0L
    }

    // 获取格式化的时间字符串
    fun getFormattedTime(): String {
        val currentElapsed = if (isRunning) {
            System.currentTimeMillis() - startTime
        } else {
            elapsedTime
        }
        val millis = currentElapsed % 1000
        val seconds = (currentElapsed / 1000) % 60
        val minutes = (currentElapsed / 60000) % 60
        return String.format("%02d:%02d:%03d", minutes, seconds, millis)
    }
}